<template>
  <div style="width: 100%; height: 100%;">
    <div id="map" class="map" style="width: 100%; height: 100%;"></div>
  </div>
</template>

<script>
import '@/assets/ol.css';
import Map from 'ol/Map';
import View from 'ol/View';
import Feature from 'ol/Feature';
import Point from 'ol/geom/Point';
import TileLayer from 'ol/layer/Tile';
import XYZ from 'ol/source/XYZ';
import LayerVector from 'ol/layer/Vector';
import SourceVector from 'ol/source/Vector';
import Overlay from "ol/Overlay";
import ImageCanvas from 'ol/source/ImageCanvas';
import Image from "ol/layer/Image";

import Text from 'ol/style/Text';
import Icon from 'ol/style/Icon';
import Fill from 'ol/style/Fill';
import Style from 'ol/style/Style';
import Circle from 'ol/style/Circle';


export default {
    components: {},
    data() {
        return {
            map: null
        };
    },
    mounted() {
        this.initMap();
    },
    methods: {
        initMap() {
            // 一个空白图层，可以理解成画布，我们要在上面绘制各种图形
            let layerVector = new LayerVector({
                source: new SourceVector()
            });

            let map = new Map({
                view: new View({
                    center: [12950000, 4860000],
                    zoom: 7
                }),
                layers: [
                    // 底图
                    new TileLayer({
                        source: new XYZ({url: 'https://server.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer/tile/{z}/{y}/{x}'})
                    }),
                    layerVector
                ],
                target: 'map',
            });

            // 一个点击事件
            map.on('click', function (evt) {
                // 鼠标点击的坐标
                let coordinate = evt.coordinate;
                console.log('click: ' + coordinate);

                map.forEachFeatureAtPixel(evt.pixel, function (feature, layer) {
                    Layers.succeed('功能建设中！');
                });
            });

            this.map = map;

            const imageSource = new ImageCanvas({
                /**
                 * 画布
                 *
                 * @param extent 范围
                 * @param resolution
                 * @param radio
                 * @param size 大小
                 * @param projection
                 * @returns {HTMLCanvasElement}
                 */
                canvasFunction: (extent, resolution, radio, size, projection) => {
                    const canvas = document.createElement('canvas');
                    const context = canvas.getContext('2d');

                    // 计算坐标转换比例
                    const scale = size[0] / (extent[2] - extent[0]);

                    // 设置画布大小
                    canvas.width = size[0];
                    canvas.height = size[1];

                    // 坐标
                    const line = [[12950000, 3860000], [12950000, 4860000]];

                    // 起始点
                    context.moveTo(-extent[0] * scale, extent[3] * scale);

                    // 转换为画布坐标
                    for (let i = 0; i < line.length; i++) {
                        const canvasX = (line[i][0] - extent[0]) * scale;
                        const canvasY = (extent[3] - line[i][1]) * scale;
                        context.lineTo(canvasX, canvasY);
                    }
                    context.stroke();

                    return canvas;
                }
            });

            const imageLayer = new Image({source: imageSource, zIndex: 2});

            // 监听地图移动事件
            map.getView().on('change:resolution', updateImageSource);
            map.getView().on('change:center', updateImageSource);

            function updateImageSource() {
                imageSource.changed(); // 触发重新渲染
            }

            map.addLayer(imageLayer);
        }
    }
};
</script>

<style scoped>
</style>
